package se.webbzon.boltzmann.game.object.action;

import java.nio.ByteBuffer;

import se.webbzon.boltzmann.environment.AbstractEnvironment;
import se.webbzon.oschi01.friendly.Friendly;
import se.webbzon.oschi01.worldobject.WorldObject;
import se.webbzon.oschi01.worldobject.WorldStaticObject;

public class ShakeActionDeflation extends AbstractActionDeflation<WorldStaticObject> {
	
	private long id;
	
	private boolean regionPersistent, gamePersistent;
	
	private ShakeActionLoot loot;
	
	private int amount;
	
	/*============================================================
	Constructors
	============================================================*/

	/** Creates a new empty sequence action deflation. **/
	public ShakeActionDeflation(	int deflationProperties,
								WorldObject[] dependencies) {
		super(deflationProperties, dependencies);
	}
	
	/** Creates a new sequence action deflation from an existing
	 * sequence action. **/
	public ShakeActionDeflation(	int deflationProperties,
								ShakeAction shakeAction) {
		super(deflationProperties, null, shakeAction);
		id = shakeAction.getId();
		regionPersistent = shakeAction.isRegionPersistent();
		gamePersistent = shakeAction.isGamePersistent();
		loot = shakeAction.getLoot();
		amount = shakeAction.getAmount();
	}
	
	/*============================================================
	Public Methods
	============================================================*/

	@Override public void inflate(ByteBuffer buff) {
		super.inflate(buff);
		
		// Read from buffer
		id = buff.getLong();
		regionPersistent = buff.get() != 0;
		gamePersistent = buff.get() != 0;
		loot = ShakeActionLoot.values()[buff.getInt()];
		amount = buff.getInt();
	}

	@Override public ByteBuffer deflate(int allocate) {
		ByteBuffer buff = super.deflate(allocate + 18);
		
		// Write to buffer
		buff.putLong(id);
		buff.put(regionPersistent ? (byte) 1 : 0);
		buff.put(gamePersistent ? (byte) 1 : 0);
		buff.putInt(loot.ordinal());
		buff.putInt(amount);
		
		return buff;
	}
	
	/*============================================================
	Friendly Methods
	============================================================*/
	
	@Friendly WorldObject makeShakeAction(AbstractEnvironment env) {
		ShakeAction a = new ShakeAction(env,getParent(),getFrontOnly(),getRange(),id);
		a.setRegionPersistent(regionPersistent);
		a.setGamePersistent(gamePersistent);
		a.setLoot(loot);
		a.setAmount(amount);
		return a; 
	}
	
}
